package com.appkubes.mall.restful.application.goods.taobao.dtkUtil;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.appkubes.mall.restful.application.goods.taobao.dtkUtil.entity.*;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

@Slf4j
public class DaTaoKeUtil {

    private static final String appSecret = "3ed7eddd1cc25ed4a5eda4e15bc104e8";//应用sercret
    private static final String appKey = "5df20b719f51a"; //应用key

    public static GetGoodsListRespond.Data<GetGoodsListRespondData> getGoodsList(int page) {
        TreeMap<String, String> paraMap = new TreeMap<>();
        paraMap.put("version", "v1.2.0");
        paraMap.put("appKey", appKey);
        paraMap.put("pageId", String.valueOf(page));
        paraMap.put("sign", SignMD5Util.getSignStr(paraMap, appSecret));
        String res = HttpUtils.sendGet("https://openapi.dataoke.com/api/goods/get-goods-list", paraMap);

        List<GetGoodsListRespondData> list = new ArrayList<>();
        GetGoodsListRespond<GetGoodsListRespondData> respond = JSONObject.parseObject(res, new TypeReference<GetGoodsListRespond<GetGoodsListRespondData>>() {
        });
        if (respond.getCode() == 0) {
            return respond.getData();
        } else {
            return null;
        }
    }

    public static GetGoodsDetailsRespond.DataBean getGoodsDetails(String goods_id) {
        TreeMap<String, String> paraMap = new TreeMap<>();
        paraMap.put("version", "v1.2.0");
        paraMap.put("appKey", appKey);
        paraMap.put("goodsId", String.valueOf(goods_id));
        paraMap.put("sign", SignMD5Util.getSignStr(paraMap, appSecret));
        String res = HttpUtils.sendGet("https://openapi.dataoke.com/api/goods/get-goods-details", paraMap);

        List<GetGoodsDetailsRespond.DataBean> list = new ArrayList<>();
        GetGoodsDetailsRespond<GetGoodsDetailsRespond.DataBean> respond = JSONObject.parseObject(res, new TypeReference<GetGoodsDetailsRespond<GetGoodsDetailsRespond.DataBean>>() {
        });
        if (respond.getCode() == 0) {
            return respond.getData();
        } else {
            return null;
        }

    }

    public static List<GetSuperCategoryRespond.DataBean> getSuperCategory() {
        TreeMap<String, String> paraMap = new TreeMap<>();
        paraMap.put("version", "v1.1.0");
        paraMap.put("appKey", appKey);
        paraMap.put("sign", SignMD5Util.getSignStr(paraMap, appSecret));
        String res = HttpUtils.sendGet("https://openapi.dataoke.com/api/category/get-super-category", paraMap);
        List<GetSuperCategoryRespond.DataBean.SubcategoriesBean> list = new ArrayList<>();
        GetSuperCategoryRespond respond = JSONObject.parseObject(res, new TypeReference<GetSuperCategoryRespond>() {
        });
        if (respond.getCode() == 0) {
            return respond.getData();
        } else {
            return null;
        }
    }

    public static GetPrivilegeLinkRespond.DataBean getPrivilegeLink(String goodsId, String pid) {
        TreeMap<String, String> paraMap = new TreeMap<>();
        paraMap.put("version", "v1.1.1");
        paraMap.put("appKey", appKey);
        paraMap.put("goodsId", goodsId);
//        paraMap.put("couponId", coupon_id);
        paraMap.put("pid", pid);
        paraMap.put("sign", SignMD5Util.getSignStr(paraMap, appSecret));
        String res = HttpUtils.sendGet("https://openapi.dataoke.com/api/tb-service/get-privilege-link", paraMap);

        GetPrivilegeLinkRespond<GetPrivilegeLinkRespond.DataBean> respond = JSONObject.parseObject(res, new TypeReference<GetPrivilegeLinkRespond<GetPrivilegeLinkRespond.DataBean>>() {
        });
        if (respond.getCode() == 0) {
            return respond.getData();
        } else {
            return null;
        }
    }

    public static List<CatalogueRespond.DataBean> catalogue() {
        TreeMap<String, String> paraMap = new TreeMap<>();
        paraMap.put("version", "v1.1.0");
        paraMap.put("appKey", appKey);
        paraMap.put("sign", SignMD5Util.getSignStr(paraMap, appSecret));
        String res = HttpUtils.sendGet("https://openapi.dataoke.com/api/goods/topic/catalogue", paraMap);

        CatalogueRespond<CatalogueRespond.DataBean> respond = JSONObject.parseObject(res, new TypeReference<CatalogueRespond<CatalogueRespond.DataBean>>() {
        });
        if (respond.getCode() == 0) {
            return respond.getData();
        } else {
            return null;
        }
    }

    public static GoodsListRespond.DataBean goodsList(Integer topic_id, Integer page, Integer page_size) {
        TreeMap<String, String> paraMap = new TreeMap<>();
        paraMap.put("version", "v1.2.0");
        paraMap.put("appKey", appKey);
        paraMap.put("topicId", topic_id.toString());
        paraMap.put("pageId", page.toString());
        paraMap.put("pageSize", page_size.toString());
        paraMap.put("sign", SignMD5Util.getSignStr(paraMap, appSecret));
        String res = HttpUtils.sendGet("https://openapi.dataoke.com/api/goods/topic/goods-list", paraMap);

        GoodsListRespond<GoodsListRespond.DataBean> respond = JSONObject.parseObject(res, new TypeReference<GoodsListRespond<GoodsListRespond.DataBean>>() {
        });
        if (respond.getCode() == 0) {
            return respond.getData();
        } else {
            return null;
        }
    }

    public static DdqRespond.DataBean getDdq(String roundTime){
        TreeMap<String, String> paraMap = new TreeMap<>();
        paraMap.put("version", "v1.2.0");
        paraMap.put("appKey", appKey);
        if(!roundTime.equals("")){
            paraMap.put("roundTime", roundTime);
        }
        paraMap.put("sign", SignMD5Util.getSignStr(paraMap, appSecret));
        String res = HttpUtils.sendGet("https://openapi.dataoke.com/api/category/ddq-goods-list", paraMap);

        DdqRespond respond = JSONObject.parseObject(res, new TypeReference<DdqRespond>() {});
        log.info("咚咚抢："+res);
        if (respond.getCode() == 0) {
            return respond.getData();
        } else {
            return null;
        }
    }

    public static List<BrandRespond.DataBean> getBrand(Integer page, Integer size){
        TreeMap<String, String> paraMap = new TreeMap<>();
        paraMap.put("version", "v1.2.0");
        paraMap.put("appKey", appKey);
        paraMap.put("pageId", page.toString());
        paraMap.put("pageSize", size.toString());
        paraMap.put("sign", SignMD5Util.getSignStr(paraMap, appSecret));
        String res = HttpUtils.sendGet("https://openapi.dataoke.com/api/tb-service/get-brand-list", paraMap);

        BrandRespond respond = JSONObject.parseObject(res, new TypeReference<BrandRespond>() {});
        log.info("咚咚抢："+res);
        if (respond.getCode() == 0) {
            return respond.getData();
        } else {
            return new ArrayList<>();
        }
    }
}
